package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.BrandMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    //查询所有
    public Result findAll() {
        List<Brand> brandList = this.list();
        return new Result(true,null,brandList);
    }

    @Override
    //添加
    public Result add(Brand brand) {
        boolean isSuccess = this.save(brand);
        if (isSuccess) {
            return new Result(true, null, null);
        }
        return new Result(false,null,null);
    }

    @Override
    //查询分页
    public Result findPage(Integer page, Integer size) {
        Page<Brand> page1 =new Page<>(page,size);
        this.page(page1);
        List<Brand> records = page1.getRecords();
        return new Result(true,null,records);
    }

    @Override
    //通过id查询
    public Result findById(Integer id) {
        Brand brand = this.getById(id);
        if (brand==null) {
            return new Result(false,null,null);
        }
        return new Result(true,null,brand);
    }

    @Override
    //更新
    public Result update(Integer id,Brand brand) {
        brand.setId(id);
        boolean isSuccess = this.updateById(brand);
        return new Result(isSuccess,null);
    }

    @Override
    //删除
    public Result delete(Integer id) {
        boolean isSuccess = this.removeById(id);
        return new Result(isSuccess,null);
    }


}
